package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    UserService userService;
    //获取当前登录（认证）用户的用户名
    @RequestMapping("/getLoginUsername")
    public Result getLoginUsername(){
        try{
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            String password = user.getPassword();
            Collection<GrantedAuthority> authorities = user.getAuthorities();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody com.itheima.pojo.User user,Integer[] checkitemIds){

        try {
            userService.edit(user,checkitemIds);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


    @RequestMapping("delete")
    public Result delete(Integer id){

        try {
            userService.delete(id);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
    @RequestMapping("add")
    public Result add(@RequestBody com.itheima.pojo.User user,Integer[] checkitemIds){

        try {
            userService.add(user,checkitemIds);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            PageResult result=userService.findPage(queryPageBean);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
          com.itheima.pojo.User user= userService.findById(id);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    /**
     *
     * 查询所有角色
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role>  roleList= userService.findAll();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,roleList);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
    @RequestMapping("/findRoleId")
    public Result findRoleId(Integer RoleId){
        try{
            List<Integer>  roleList= userService.findByRoleId(RoleId);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,roleList);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
}
